home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / tcp / amitcp / amitcp-api-22.lha / AmiTCP-2.2 / src / netlib / autoinit.c next >
Encoding:
C/C++ Source or Header  |  1993-10-18  |  3.6 KB  |  144 lines

  1. RCS_ID_C="$Id: autoinit.c,v 1.10 1993/10/18 05:53:01 jraja Exp $";
  2. /*
  3.  * autoinit.c --- SAS C auto initialization functions
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  8.  *                  Helsinki University of Technology, Finland.
  9.  *                  All rights reserved.
  10.  *
  11.  * Created      : Sat Mar 20 03:31:29 1993 ppessi
  12.  * Last modified: Mon Jul 19 14:24:30 1993 jraja
  13.  *
  14.  */
  15.  
  16. #include <exec/types.h>
  17. #include <exec/libraries.h>
  18.  
  19. #include <intuition/intuition.h>
  20.  
  21. #include <proto/socket.h>
  22. #include <proto/exec.h>
  23. #include <proto/intuition.h>
  24.  
  25. #include <stdlib.h>
  26.  
  27. struct Library *SocketBase = NULL;
  28.  
  29. int errno = 0;            /* global errno variable */
  30.  
  31. STRPTR SOCKETNAME = "bsdsocket.library";
  32.  
  33. #define SOCKETVERSION 2        /* minimum bsdsocket version to use */
  34.  
  35. extern STRPTR _ProgramName;    /* SAS startup module defines this :-) */
  36.  
  37. /****** net.lib/autoinit *********************************************
  38. *
  39. *   NAME   
  40. *       autoinit - SAS C Autoinitialization Functions 
  41. *
  42. *   SYNOPSIS
  43. *       _STIopenSockets()
  44. *
  45. *       void _STIopenSockets(void)
  46. *
  47. *       _STDcloseSockets()
  48. *
  49. *       void _STDcloseSockets(void)
  50. *
  51. *   FUNCTION
  52. *       These functions open and close the bsdsocket.library at the
  53. *       startup and exit of the program, respectively. For a
  54. *       program to use these functions, it must be linked with
  55. *       netlib:net.lib.
  56. *
  57. *       If the library can be opened, the _STIopenSockets() calls
  58. *       bsdsocket.library function SetErrnoPtr() to tell the
  59. *       library the address and the size of the errno variable of
  60. *       the calling program. 
  61. *
  62. *   NOTES
  63. *       _STIopenSockets() also checks that the system version is at
  64. *       least 37. It puts up a requester if the bsdsocket.library
  65. *       is not found or is of wrong version.
  66. *
  67. *       The autoinitialization and autotermination functions are
  68. *       features specific to the SAS C6. However, these functions
  69. *       can be used with other (ANSI) C compilers, too. Example
  70. *       follows: 
  71. *
  72. *       \* at start of main() *\
  73. *
  74. *       atexit(_STDcloseSockets);
  75. *       _STDopenSockets();
  76. *
  77. *   BUGS
  78. *
  79. *   SEE ALSO
  80. *       bsdsocket.library/SetErrnoPtr(),
  81. *       SAS/C 6 User's Guide p. 145 for details of
  82. *       autoinitialization and autotermination functions.  
  83. *****************************************************************************
  84. *
  85. */
  86.  
  87. /*
  88.  * Using __stdargs prevents creation of register arguments entry point.
  89.  * If both stack args and reg. args entry points are created, this
  90.  * function is called _twice_, which is not wanted.
  91.  */
  92. void __stdargs
  93. _STIopenSockets(void)
  94. {
  95.   struct Library *IntuitionBase;
  96.   STRPTR errorStr;
  97.  
  98.   /*
  99.    * Check OS version
  100.    */
  101.   if ((*(struct Library **)4)->lib_Version < 37)
  102.     exit(20);
  103.  
  104.   /*
  105.    * Open bsdsocket.library
  106.    */
  107.   if ((SocketBase = OpenLibrary(SOCKETNAME, SOCKETVERSION)) != NULL) {
  108.     /*
  109.      * Succesfull. Now tell bsdsocket.library the address of our errno
  110.      */
  111.     SetErrnoPtr(&errno, sizeof(errno));
  112.     
  113.     return;
  114.   }
  115.   else
  116.     errorStr = "AmiTCP/IP version 2 or later must be started first.";
  117.   
  118.   IntuitionBase = OpenLibrary("intuition.library", 36);
  119.   
  120.   if (IntuitionBase != NULL) {
  121.     struct EasyStruct libraryES;
  122.     
  123.     libraryES.es_StructSize = sizeof(libraryES);
  124.     libraryES.es_Flags = 0;
  125.     libraryES.es_Title = _ProgramName;
  126.     libraryES.es_TextFormat = errorStr;
  127.     libraryES.es_GadgetFormat = "Exit %s";
  128.     
  129.     EasyRequestArgs(NULL, &libraryES, NULL, (APTR)&_ProgramName);
  130.  
  131.     CloseLibrary(IntuitionBase);
  132.   }
  133.   exit(20);
  134. }
  135.  
  136. void __stdargs
  137. _STDcloseSockets(void)
  138. {
  139.   if (SocketBase) {
  140.     CloseLibrary(SocketBase);
  141.     SocketBase = NULL;
  142.   }
  143. }
  144.